package com.study.d012Components;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.Clipboard;
import javafx.scene.input.ClipboardContent;
import javafx.scene.input.DataFormat;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyCodeCombination;
import javafx.scene.input.KeyCombination;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

/**
*@description  粘贴板的使用
*@author xtayhame
*date 2020年3月8日
*version
*since v1.0
*/
public class ClipboardMain extends Application{
	public static void main(String[] args) {
		launch(args);
	}
	@Override
	public void start(Stage stage) throws Exception {

		Clipboard clipboard = Clipboard.getSystemClipboard();
		ClipboardContent cc=new ClipboardContent();
		//放图片
		cc.putImage(new Image(new FileInputStream(new File("E:\\javafxworkspace\\lianxidemo\\src\\icon\\01.jpg"))));

		//放文件
		/*List<File> listFiles = new ArrayList<File>();
		listFiles.add(new File("E:\\javafxworkspace\\lianxidemo\\src\\icon\\01.jpg"));
		cc.putFiles(listFiles);*/

		cc.putString("从粘贴板读取数据");


		clipboard.setContent(cc);


		AnchorPane anchorPane = new AnchorPane();
		//hBox.setStyle("-fx-background-color:#fff333");
		Label tf = new Label();
		ImageView iv = new ImageView();
		iv.setFitHeight(200);
		iv.setFitHeight(200);
		iv.setStyle("-fx-background-color:#fff333");
		anchorPane.getChildren().addAll(tf,iv);
		AnchorPane.setLeftAnchor(iv, 100.0);
		stage.setScene(new Scene(anchorPane, 400, 500));
		stage.show();


		KeyCombination kc = new KeyCodeCombination(KeyCode.V, KeyCodeCombination.CONTROL_DOWN);
		tf.getScene().getAccelerators().put(kc, new Runnable() {
			@Override
			public void run() {
				System.out.println("hasFiles=" + clipboard.hasFiles());
				System.out.println("hasString=" + clipboard.hasString());
				System.out.println("hasImage=" + clipboard.hasImage());

				if(clipboard.hasString()){
					System.out.println("文字");
					tf.setText(clipboard.getString());
				}
				if(clipboard.hasImage()){
					System.out.println("图片");
					iv.setImage(clipboard.getImage());
					iv.setPreserveRatio(true);
					iv.setFitHeight(200);
				}
				if(clipboard.hasFiles()){
					//获取图片或文件是 可以用hasFiles()进行判断，   用hasImage()判断无需 在win10上
					try {
						System.out.println("图片");
						File file= clipboard.getFiles().get(0);
						iv.setImage(new Image(new FileInputStream(file)));
						iv.setPreserveRatio(true);
						iv.setFitHeight(200);
					} catch (FileNotFoundException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		});

	}
}
